package com.cskaoyan.controller.admin;

import com.cskaoyan.anno.AddLog;
import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.admin.AdminAuthService;
import com.cskaoyan.token.MarketToken;
import com.cskaoyan.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {


    @Autowired
    AdminAuthService adminAuthService;

    /**
     * 登录功能
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo<com.cskaoyan.bean.LoginUserData>
     * @author Bowen Deng
     * @since 2022/06/27 16:04
     */

    @AddLog(value = "登陆", comment = "管理员登陆", result = "result test")
    @PostMapping("login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        Subject subject = SecurityUtils.getSubject();

        MarketToken marketToken = new MarketToken(username, MD5Util.MD5EncodeUtf8(password), "admin");
        try {
            subject.login(marketToken);
        } catch (AuthenticationException e) {

        }
        if (subject.isAuthenticated()) {
            MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
            LoginUserData loginUserData = new LoginUserData();
            AdminInfoBean adminInfo = new AdminInfoBean();
            adminInfo.setAvatar(primaryPrincipal.getAvatar());
            adminInfo.setNickName(primaryPrincipal.getUsername());
            loginUserData.setAdminInfo(adminInfo);
            Session session = subject.getSession();
            String sessionId = (String) session.getId();
            loginUserData.setToken(sessionId);
            return BaseRespVo.ok(loginUserData);
        }

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(401);
        baseRespVo.setErrmsg("登陆失败，请重试");
        return baseRespVo;
    }


    /**
     * 登陆之后显示所有的信息
     *
     * @param token
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/29 10:58 上午
     */
    @RequestMapping("info")
    public BaseRespVo info(String token) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        InfoData infoData = new InfoData();
        if (!principals.isEmpty()) {
            MarketAdmin primaryPrincipal = (MarketAdmin) principals.getPrimaryPrincipal();
            infoData.setName(primaryPrincipal.getUsername());
            infoData.setAvatar(primaryPrincipal.getAvatar());

            List<String> permissions = adminAuthService.getPermissionsApi(primaryPrincipal.getRoleIds());
            List<String> roles = adminAuthService.getRolesByAdminId(primaryPrincipal.getRoleIds());
            infoData.setPerms(permissions);
            infoData.setRoles(roles);
        }
        //根据primaryPrincipal做查询
        return BaseRespVo.ok(infoData);
    }

    /**
     * 登出功能
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Yan Dexuan
     * @since 2022/06/27 16:04
     */
    @AddLog(value = "登出", comment = "管理员登出", result = "result test")
    @RequestMapping("logout")
    public BaseRespVo logout() {

        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return BaseRespVo.ok();
    }

    /**
     * 认证失败的跳转
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/29 12:41 上午
     */

    @RequestMapping("401")
    public BaseRespVo noAuth() {
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(401);
        baseRespVo.setErrmsg("认证失败，拒绝访问");
        return baseRespVo;
    }

}
